﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;
using Yipusoft.BasicBase.BLL;
using Yipusoft.BasicBase.Model;
using System.Collections.Generic;
using System.Text;
using Yipusoft.Account.Model;
using Yipusoft.Account.BLL;
using System.IO;

public partial class BasicBase_DeviceNew : System.Web.UI.Page
{

    int _limitwidth = 640;
    int _limitheight = 480;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDepartment();
            BindType();
            BindClass();
            BindCodeItemList(UnitNameList, "ZBQC020000");
            BindCodeItemList(StateList, "ZBQC030000");
            UnitList.SelectedValue = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
            if (Request.QueryString["DeviceID"] == null)
            {
                if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "200500") || Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "200501"))
                {
                    ActionTitle.Text = "添加装备器材";
                }
                else
                {
                    Response.Write("<script>alert('没有权限！');history.back(0);</script>");
                    return;
                }


            }
            else
            {
                if (!Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "200500") && !Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "200502"))
                {
                    Response.Write("<script>alert('没有权限！');history.back(0);</script>");
                    return;
                }
                else
                {
                    ActionTitle.Text = "编辑装备器材";
                    BindData();
                }
            }




        }
    }

    void BindDepartment()
    {
        UserInfo userInfo = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
        DepartmentInfo department = Department.GetDepartment(userInfo.DepartmentID);
        if (userInfo.DepartmentID.Substring(0, 4) == "1300")
        {
            DepartmentList.DataSource = Yipusoft.Account.BLL.Department.GetList(" (SUBSTRING(DepartmentID, 5, 3) = '000') and DepartmentID<>'1300000' ");
            DepartmentList.DataBind();
            DepartmentListBind();
        }
        else
        {
            DepartmentList.DataSource = Yipusoft.Account.BLL.Department.GetList(" DepartmentID= '" + department.DepartmentID.Substring(0, 4) + "000' ");
            DepartmentList.DataBind();
            DepartmentListBind();
        }
    }

    private void BindCodeItemList(DropDownList ddl, string parentID)
    {
        ListItem li;
        List<CodeItemInfo> items = CodeItem.GetList("ParentID='" + parentID + "' ORDER BY ItemID,SeqID");
        foreach (CodeItemInfo item in items)
        {
            li = new ListItem();
            li.Text = "".PadRight(2 * (item.ItemLevel - 1), '　') + item.ItemName;
            li.Value = item.ItemID;
            ddl.Items.Add(li);
            BindCodeItemList(ddl, item.ItemID);
        }
    }

    void BindType()
    {
        DeviceTypeList.DataTextField = "ItemName";
        DeviceTypeList.DataValueField = "ItemID";
        DeviceTypeList.DataSource = CodeItem.GetList(" parentID='ZBQC010000' order by ItemID,SeqID ");
        DeviceTypeList.DataBind();
    }
    private void BindData()
    {
        DeviceInfo device = Device.GetDevice(new Guid(Request.QueryString["DeviceID"]));
        DepartmentList.SelectedValue = device.DepartmentID.Substring(0, 4) + "000";
        UnitList.SelectedValue = device.DepartmentID;
        //CascadingDropDown_Department.SelectedValue = device.DepartmentID.Substring(0, 4) + "000";
        //CascadingDropDown_Unit.SelectedValue = device.DepartmentID;
        if (device.DeviceClass != "")
        {
            DeviceTypeList.SelectedValue = CodeItem.GetCodeItem(device.DeviceClass).ParentID;
            DeviceClassList.SelectedValue = device.DeviceClass;
        }
        DeviceName.Text = device.DeviceName;
        DeviceModel.Text = device.DeviceModel;
        DeviceCount.Text = device.DeviceCount.ToString();
        UnitNameList.SelectedIndex = UnitNameList.Items.IndexOf(UnitNameList.Items.FindByText(device.UnitName));
        Parameters.Text = device.Parameters;
        Price.Text = device.Price.ToString();
        DatePurchase.Text = device.DatePurchase.ToShortDateString();
        DateUse.Text = device.DateUse.ToShortDateString();
        DateProduct.Text = device.DateProduct.ToShortDateString();
        Manufacturer.Text = device.Manufacturer;
        StateList.SelectedIndex = StateList.Items.IndexOf(StateList.Items.FindByValue(device.State));
    }
    private void Insert()
    {

        DeviceInfo device = new DeviceInfo();
        Guid deviceID = Guid.NewGuid();
        string path = Server.MapPath("pics\\");
        string word = "";
        if (WordFile.HasFile)
        {
            string ex = WordFile.FileName.Substring(WordFile.FileName.LastIndexOf('.') + 1);
            if (ex.ToLower() != "jpg" && ex.ToLower() != "png" && ex.ToLower() != "gif" && ex.ToLower() != "bmp")
            {
                Prompt.Text = "提示:上传图片格式错误!";
                return;
            }
            else
            {
                if (WordFile.FileContent.Length / 1024 / 1024 > 5)
                {

                    Prompt.Text = "提示:上传文档不可大于5M!";
                    return;
                }
                else
                {
                    Stream oStream = WordFile.PostedFile.InputStream;
                    System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);

                    int owidth = oImage.Width; //原图宽度
                    int oheight = oImage.Height; //原图高度

                    if (owidth > _limitwidth || oheight > _limitheight)
                    {

                        Prompt.Text = "超过允许的图片尺寸(" + _limitwidth.ToString() + "×" + _limitheight.ToString() + ")范围!";
                        return;

                    }//检查是否超出规定尺寸

                    word = DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + "." + ex;

                    WordFile.SaveAs(path + word);

                }
            }
        }
        string where = " DepartmentID='" + UnitList.SelectedValue + "' and DeviceClass='" + DeviceClassList.SelectedValue + "' and DeviceName='" + DeviceName.Text + "' and State='" + StateList.SelectedValue + "' ";
        List<DeviceInfo> list = Device.GetList(where);
        if (list.Count > 0)
        {
            Prompt.Text = "提示:此装备器材已存在!";
            return;
        }
        device.Picture = word;
        device.DeviceID = deviceID;
        device.DepartmentID = UnitList.SelectedValue;
        device.DeviceClass = DeviceClassList.SelectedValue;
        device.DeviceName = DeviceName.Text;
        device.DeviceModel = DeviceModel.Text;
        device.DeviceCount = int.Parse(DeviceCount.Text);
        device.UnitName = UnitNameList.SelectedValue;
        device.Parameters = Parameters.Text;
        device.Price = decimal.Parse(Price.Text);
        device.DatePurchase = DateTime.Parse(DatePurchase.Text);
        device.DateUse = DateTime.Parse(DateUse.Text);
        device.DateProduct = DateTime.Parse(DateProduct.Text);
        device.Manufacturer = Manufacturer.Text;
        device.State = StateList.SelectedValue;
        device.TimeLastModified = DateTime.Now;
        Device.Insert(device);

        EventLogInfo log = new EventLogInfo();
        log.EventTime = DateTime.Now;
        log.EventType = 1;
        log.EventData = "添加新记录";
        log.ItemID = deviceID.ToString();
        log.ItemName = "Devices";
        log.ItemFullUrl = "DeviceNew.aspx";
        log.CreatedBy = Context.User.Identity.Name;
        log.CreatedTime = DateTime.Now;
        log.UserIP = Request.UserHostAddress;
        EventLog.Insert(log);

        Prompt.Text = "提示：添加成功！";
    }
    private void Update()
    {
        DeviceInfo device = Device.GetDevice(new Guid(Request.QueryString["DeviceID"]));
        string path = Server.MapPath("pics\\");
        string word = device.Picture;
        if (WordFile.HasFile)
        {
            string ex = WordFile.FileName.Substring(WordFile.FileName.LastIndexOf('.') + 1);
            if (ex.ToLower() != "jpg" && ex.ToLower() != "png" && ex.ToLower() != "gif" && ex.ToLower() != "bmp")
            {
                Prompt.Text = "提示:上传图片格式错误!";
                return;
            }
            else
            {
                if (WordFile.FileContent.Length / 1024 / 1024 > 5)
                {

                    Prompt.Text = "提示:上传文档不可大于5M!";
                    return;
                }
                else
                {
                    Stream oStream = WordFile.PostedFile.InputStream;
                    System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);

                    int owidth = oImage.Width; //原图宽度
                    int oheight = oImage.Height; //原图高度

                    if (owidth > _limitwidth || oheight > _limitheight)
                    {

                        Prompt.Text = "超过允许的图片尺寸(" + _limitwidth.ToString() + "×" + _limitheight.ToString() + ")范围!";
                        return;

                    }//检查是否超出规定尺寸
                    if (word != "")
                    {
                        FileInfo file = new FileInfo(path + word);
                        if (file.Exists)
                        {
                            file.IsReadOnly = false;
                            file.Delete();
                        }
                    }

                    word = DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + "." + ex;

                    WordFile.SaveAs(path + word);
                    device.Picture = word;
                }
            }
        }
        device.DepartmentID = UnitList.SelectedValue;
        device.DeviceClass = DeviceClassList.SelectedValue;
        device.DeviceName = DeviceName.Text;
        device.DeviceModel = DeviceModel.Text;
        device.DeviceCount = int.Parse(DeviceCount.Text);
        device.UnitName = UnitNameList.SelectedValue;
        device.Parameters = Parameters.Text;
        device.Price = decimal.Parse(Price.Text);
        device.DatePurchase = DateTime.Parse(DatePurchase.Text);
        device.DateUse = DateTime.Parse(DateUse.Text);
        device.DateProduct = DateTime.Parse(DateProduct.Text);
        device.Manufacturer = Manufacturer.Text;
        device.State = StateList.SelectedValue;
        device.TimeLastModified = DateTime.Now;
        Device.Update(device);

        EventLogInfo log = new EventLogInfo();
        log.EventTime = DateTime.Now;
        log.EventType = 1;
        log.EventData = "更新记录";
        log.ItemID = Request.QueryString["DeviceID"];
        log.ItemName = "Devices";
        log.ItemFullUrl = "DeviceNew.aspx";
        log.CreatedBy = Context.User.Identity.Name;
        log.CreatedTime = DateTime.Now;
        log.UserIP = Request.UserHostAddress;
        EventLog.Insert(log);

        Prompt.Text = "提示：更新成功！";
    }
    protected void ButtonSave_Click(object sender, EventArgs e)
    {
        try
        {
            if (Request.QueryString["DeviceID"] == null)
                Insert();
            else
                Update();
        }
        catch (Exception ex)
        {
            Prompt.Text = "错误：" + ex.Message;
        }
    }
    protected void DepartmentList_TextChanged(object sender, EventArgs e)
    {
        DepartmentListBind();
    }

    void DepartmentListBind()
    {
        string strWhere = "DepartmentID like '" + DepartmentList.SelectedValue.Substring(0, 4) + "%' and   SUBSTRING(DepartmentID, 5, 1)<>'0'";
        UnitList.DataSource = Department.GetList(strWhere);
        UnitList.DataBind();
    }
    protected void DeviceTypeList_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindClass();
    }

    private void BindClass()
    {
        DeviceClassList.DataTextField = "ItemName";
        DeviceClassList.DataValueField = "ItemID";
        DeviceClassList.DataSource = CodeItem.GetList(" parentID='" + DeviceTypeList.SelectedValue + "' order by ItemID,SeqID ");
        DeviceClassList.DataBind();
    }
}
